package users

import (
	"api/internal/respons"
	"context"
	"smart-health-platforms/pkg"
	"strconv"
	"user_srv/userclient"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

// @Summary 用户登录注册一体化
// @Description 用户通过手机号，验证码和密码注册用户,手机号密码登录
// @Tags 认证
// @Accept json
// @Produce json
// @Param data body types.InterGRationRequest true "登录信息"
// @Success 200 {object} types.Response
// @Failure 400 {string} types.Response
// @Failure 500 {string} types.Response
// @Router /users/interGRation [post]
type InterGRationLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewInterGRationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InterGRationLogic {
	return &InterGRationLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// 注册登录一体化
func (l *InterGRationLogic) InterGRation(req *types.InterGRationRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	user, err := l.svcCtx.UserRpc.InterGRation(l.ctx, &userclient.InterGRationRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
		SmsCode:  req.SendSms,
	})
	if err != nil {
		return respons.Error(400, "服务端错误", err.Error()), err
	}
	//登录成功，生成token
	token, err := pkg.TokenHandler(strconv.FormatInt(user.Id, 10))
	if err != nil {
		return respons.Error(400, "token生成失败", err.Error()), err
	}
	return respons.Success(token), nil
}
